
#ifndef __GD25Q64_H
#define __GD25Q64_H

#include "gd32f30x.h"

#define gd25q64RCU_SPIx   	 RCU_SPI0
#define gd25q64RCU_GPIOx  	 RCU_GPIOA
#define gd25q64GPIOx      	 GPIOA
#define gd25q64GPIO_PIN_x  	 GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7
#define gd25q64SPI_CS_PIN_x  GPIO_PIN_4
 
#define gd25q64DataLen 4096
 
 
#define GD25Q64_WriteEnable      0x06
#define GD25Q64_WriteDisable     0x04
#define GD25Q64_ReadStatusRegister1 0x05  //S7-S0
#define GD25Q64_ReadStatusRegister2 0x35  //S15-S8
#define GD25Q64_ReadStatusRegister3 0x15  //S23-S16
#define GD25Q64_WriteStatusRegister1 0x01 
#define GD25Q64_WriteStatusRegister2 0x31 
#define GD25Q64_WriteStatusRegister3 0x11 
#define GD25Q64_ReadData  0x03
#define GD25Q64_FastRead  0x0B
#define GD25Q64_PageProgram 0x02
#define GD25Q64_QuadPageProgram 0x32
#define GD25Q64_SectorErase 0x20
#define GD25Q64_BlockErase32 0x52
#define GD25Q64_BlockErase64 0xD8
#define GD25Q64_ChipErase 0xC7
#define GD25Q64_ReadIdentification 0x9F
#define GD25Q64_DeepPowerDown  0xB9
#define GD25Q64_ManufacturerDeviceID 0x90
#define GD25Q64_ReleaseFromDeepPowerDown 0xAB
#define GD25Q64_ReadIdentification 0x9F

//Program/Erase Speed
#define PageProgramTime 0.6   //ms
#define SectorEraseTime 50
#define BlockEraseTime  200
#define ChipEraseTime   25000

//Flexible Architecture   1M=1024K  1K=1024Byte 1Byte=8bit 
#define GD25Q64_Size       (8*1024*1024)
#define GD25Q64_PageSize    256      //Byte
#define GD25Q64_SectorSize (4*1024) //Byte
#define GD25Q64_BlockSize  (32*1024) //Byte



#define GD25Q64_ADDRESS_DEVINFO 					((uint32_t)0x00)
#define GD25Q64_ADDRESS_TASK    					((uint32_t)0x1000)
#define GD25Q64_ADDRESS_GLOBALTASK  			((uint32_t)0x41000)
#define GD25Q64_ADDRESS_BOOTLOADER_INFO 	((uint32_t)0x81000)
#define GD25Q64_ADDRESS_BOOTLOADER_APP1 	((uint32_t)0x82000)
#define GD25Q64_ADDRESS_BOOTLOADER_BACKUP ((uint32_t)0xA2000)



#define GD25Q64_CS_LOW()			{gpio_bit_write(GPIOA, GPIO_PIN_4,RESET);}
#define GD25Q64_CS_HIGH() 			{gpio_bit_write(GPIOA, GPIO_PIN_4,SET);}


void vgd25q64SPIInit(void);
void vgd25q64EraseSector(uint32_t eraseAddr);
void vgd25q64EraseAppointSector(uint32_t addstart,uint32_t addend);
void Flash_EraseChip(void);

void vgd25q64ReadData(uint8_t *sourcedata, uint32_t address, uint32_t length);
void vgd25q64WriteData(uint8_t *sourcedata, uint32_t address, uint32_t length);
void vgd25q64FlushWrite(uint8_t	*sourcedata,uint32_t address,uint32_t length);
void vgd25q64FlushDel(uint32_t address,uint32_t length);

#endif

